//
//  ZQJMessageCell.m
//  QQ聊天界面
//
//  Created by 3tee on 2021/6/7.
//  Copyright © 2021年 3tee. All rights reserved.
//

#import "ZQJMessageCell.h"
#import "ZQJMessage.h"

@interface ZQJMessageCell()

@property (nonatomic, weak) UILabel *labTime;
@property (nonatomic, weak) UILabel *labName;
@property (nonatomic, weak) UIButton *btnContent;
@property (nonatomic, weak) UIImageView *imgIcon;

@end

@implementation ZQJMessageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor clearColor];
        
        UILabel *labName = [[UILabel alloc]init];
        labName.font = [UIFont systemFontOfSize:10];
        labName.textAlignment = NSTextAlignmentCenter;
        self.labName = labName;
        
        UILabel *labTime = [[UILabel alloc]init];
        labTime.font = [UIFont systemFontOfSize:10];
        labTime.textAlignment = NSTextAlignmentCenter;
        self.labTime = labTime;
        UIButton *btnContent = [UIButton buttonWithType:UIButtonTypeCustom];
//        btnContent.backgroundColor = [UIColor redColor];
        btnContent.titleLabel.font = [UIFont systemFontOfSize:13];
        btnContent.contentEdgeInsets = UIEdgeInsetsMake(15, 20, 15, 20);//设置内边距为了让文字显示在里面
        btnContent.titleLabel.numberOfLines = 0;
        self.btnContent = btnContent;
        UIImageView *imgIcon = [[UIImageView alloc]init];
        self.imgIcon = imgIcon;
        [self.contentView addSubview:self.labName];
        [self.contentView addSubview:self.labTime];
        [self.contentView addSubview:self.btnContent];
        [self.contentView addSubview:self.imgIcon];
    }
    return self;
}

- (void)setMsgFrame:(ZQJMessageFrame *)msgFrame{
    _msgFrame = msgFrame;
    self.labName.frame = msgFrame.nameFrame;
    self.labName.textAlignment =  msgFrame.message.type == ZQJMessageOther ? NSTextAlignmentLeft : NSTextAlignmentRight;
    self.labName.text = msgFrame.message.name;
    self.labTime.text = msgFrame.message.time;
    self.labTime.frame = msgFrame.timeFrame;
    NSString *icon = msgFrame.message.type == ZQJMessageOther ? @"ic_other":@"ic_me";
    self.imgIcon.image = [UIImage imageNamed:icon];
    self.imgIcon.frame = msgFrame.iconFrame;
    [self.btnContent setTitle:msgFrame.message.msg forState:UIControlStateNormal];
    UIColor *color = msgFrame.message.type == ZQJMessageMe ? [UIColor blueColor] : [UIColor blackColor];
    [self.btnContent setTitleColor:color forState:UIControlStateNormal];
    NSString *imageName = msgFrame.message.type == ZQJMessageMe ? @"ic_bubble_send":@"ic_bubble_received";
    UIImage *img = [UIImage imageNamed:imageName];
    [img stretchableImageWithLeftCapWidth:img.size.width/2 topCapHeight:img.size.height/2];
    [self.btnContent setBackgroundImage:img forState:UIControlStateNormal];
    self.btnContent.frame = msgFrame.contentFrame;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
